package com.feihong.yw189.bluetooth.utils.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * SharedPreference的工具类
 * @author zhengpeng
 */
public class PrefUtils {

    private static SharedPreferences sp;

    public static boolean getBoolean(Context context, String key,
                                     boolean defaultValue) {
        SharedPreferences sp = context.getSharedPreferences("config",
                Context.MODE_PRIVATE);
        return sp.getBoolean(key, defaultValue);
    }

    public static void setBoolean(Context context, String key, boolean value) {
        SharedPreferences sp = context.getSharedPreferences("config",
                Context.MODE_PRIVATE);
        Editor edit = sp.edit();
        edit.putBoolean(key, value);
        edit.commit();
    }

    public static String getString(Context context, String key,
                                   String defaultValue) {
        if (sp == null){
            sp = context.getSharedPreferences("config",
                    Context.MODE_PRIVATE);
        }

        return sp.getString(key, defaultValue);
    }

    public static void setString(Context context, String key, String value) {
        SharedPreferences sp = context.getSharedPreferences("config",
                Context.MODE_PRIVATE);
        Editor edit = sp.edit();
        edit.putString(key, value);
        edit.commit();
    }

    public static int getInt(Context context, String key, int defaultValue) {
        SharedPreferences sp = context.getSharedPreferences("config",
                Context.MODE_PRIVATE);
        return sp.getInt(key, defaultValue);
    }

    public static void setInt(Context context, String key, int value) {
        SharedPreferences sp = context.getSharedPreferences("config",
                Context.MODE_PRIVATE);
        Editor edit = sp.edit();
        edit.putInt(key, value);
        edit.commit();
    }


    /**
     * 删除指定数据
     * @param context
     * @param key
     */
    public static void removeString(Context context,String key){
        SharedPreferences sharedPreferences = context.getSharedPreferences("config",Context.MODE_PRIVATE);
        sharedPreferences.edit().remove(key).commit();
    }

    // 创建删除方法：
    public static void clearData(Context context) {
        SharedPreferences sp = context.getSharedPreferences("config",
                Context.MODE_PRIVATE);

        sp.edit().clear().commit();
    }
    /**
     * 根据图片的url路径获得Bitmap对象
     *
     * @param url
     * @return
     */
//    private Bitmap returnBitmap(String url) {
//        URL fileUrl = null;
//        Bitmap bitmap = null;
//
//        try {
//            fileUrl = new URL(url);
//        } catch (MalformedURLException e) {
//            e.printStackTrace();
//        }
//
//        try {
//            HttpURLConnection conn = (HttpURLConnection) fileUrl
//                    .openConnection();
//            conn.setDoInput(true);
//            conn.connect();
//            InputStream is = conn.getInputStream();
//            bitmap = BitmapFactory.decodeStream(is);
//            is.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return bitmap;
//
//    }

}
